package com.qsmx.store.app.main.goods.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.qsmx.store.app.R;
import com.qsmx.store.app.main.goods.entity.GoodsCreateOrderEntity;
import com.qsmx.store.app.main.goods.holder.GoodsCreateOrderHolder;

import java.util.List;

public class GoodsCreateAdapter extends RecyclerView.Adapter<GoodsCreateOrderHolder> {

    private Context mContext;
    private List<GoodsCreateOrderEntity> mData;

    //定义接口
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemMin(View view, int position);

        void onItemPlus(View view, int position);

        void onItemDelete(View view, int position);

    }

    //声明自定义的监听接口
    private GoodsCreateAdapter.OnItemClickListener monItemClickListener = null;

    //提供set方法
    public void setonItemClickListener(GoodsCreateAdapter.OnItemClickListener listener) {
        this.monItemClickListener = listener;
    }

    public void setData(List<GoodsCreateOrderEntity> mData) {
        this.mData = mData;
    }

    public GoodsCreateAdapter(Context mContext) {
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public GoodsCreateOrderHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new GoodsCreateOrderHolder(LayoutInflater.from(mContext).inflate(R.layout.adapter_create_order, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull GoodsCreateOrderHolder holder, final int position) {

        final GoodsCreateOrderEntity data = mData.get(position);

        holder.tvGoodsName.setText(data.getGoodsName());
        holder.tvGoodsPrice.setText(String.valueOf(data.getPrice()));
        holder.tvGoodsNum.setText(String.valueOf(data.getDetailNum()));
        holder.tvAllPrice.setText(String.valueOf(data.getPrice() * data.getDetailNum()));

        holder.tvGoodsMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                monItemClickListener.onItemMin(v,position);
            }
        });

        holder.tvGoodsPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                monItemClickListener.onItemPlus(v,position);

            }
        });

        holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                monItemClickListener.onItemDelete(v,position);

            }
        });

    }

    @Override
    public int getItemCount() {
        if (mData != null && mData.size() > 0) {
            return mData.size();
        } else {
            return 0;
        }
    }
}
